#include <qpe/config.h>
#include <qevent.h>

#include "MouseWidget.h"

MouseWidget::MouseWidget(Config *conf, QWidget *parent): QWidget(parent),
		config(conf)
{}

void MouseWidget::mousePressEvent(QMouseEvent *e)
{
	startPoint = prevPoint = e->pos();
	startTime = time(NULL);
}

void MouseWidget::mouseReleaseEvent(QMouseEvent *e)
{
	config->setGroup("Mouse");
	if (!config->readBoolEntry("EnableTapClick"))
		return;
	
	if (1 < difftime(time(NULL), startTime))
		return;
	
	QPoint p = e->pos();
	int margin = config->readNumEntry("TapClickMargin");
	if (margin < (p.x() - startPoint.x()) || margin < (p.y() - startPoint.y()))
		return;
	
	emit mouseLeftButtonClicked();
}

void MouseWidget::mouseMoveEvent(QMouseEvent *e)
{
	const QPoint &p = e->pos();
	
	int x = p.x();
	int y = p.y();
	
	emit mouseMovedAbsolute(static_cast<short>((double)x / size().width() * USHRT_MAX), static_cast<short>((double)y / size().height() * USHRT_MAX));
	emit mouseMovedRelative(x - prevPoint.x(), y - prevPoint.y());
	
	prevPoint = p;
}

/*
void MouseWidget::keyPressEvent(QKeyEvent *e)
{
	if (e->key() == Qt::Key_Shift)
		rightButton = true;
}

void MouseWidget::keyReleaseEvent(QKeyEvent *e)
{
	if (e->key() == Qt::Key_Shift)
		rightButton = false;
}
*/
